// Values.cpp : Implementation of CValues

#include "stdafx.h"
#include "Values.h"


// CValues

STDMETHODIMP CValues::get__NewEnum(IUnknown** ppUnk)
{
	if (ppUnk == NULL)
	{
		return E_POINTER;
	}
	*ppUnk = NULL;
	 return this->QueryInterface(__uuidof(IUnknown), (void **)ppUnk);;
}

STDMETHODIMP CValues::get_Count(LONG* pVal)
{
	*pVal = (LONG)m_Bstrs.size();
	return S_OK;
}

STDMETHODIMP CValues::get_Item(LONG Index, BSTR* pVal)
{
	LONG count;
	get_Count(&count);
	if (Index >= count)
	{
		Error(L"Out of index!");
		return E_FAIL;
	}
	_bstr_t t = m_Bstrs[Index];
	*pVal = t.Detach();

	return S_OK;
}

STDMETHODIMP CValues::Next(ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
{	
	if (pCeltFetched != NULL)
	{
		*pCeltFetched = 0;
	}
	for (ULONG i = 0;i < celt; i++)
	{
		if (S_FALSE == Skip(1))
		{
			return S_FALSE;
		}
		BSTR val = m_Bstrs[m_EnumIndex];
		rgVar[i] = _variant_t(val).Detach();
		//rgVar[i] = _variant_t(val).Detach();

		rgVar ++;
		if (pCeltFetched != NULL)
		{
			(*pCeltFetched) ++;
		}
	}

	return S_OK;
}

STDMETHODIMP CValues::Skip(ULONG celt)
{
	m_EnumIndex += celt;
	LONG count;
	get_Count(&count);
	if (m_EnumIndex >= count)
	{
		return S_FALSE;
	}
	return S_OK;
}

STDMETHODIMP CValues::Reset(void)
{
	m_EnumIndex = -1;
	return S_OK;
}

STDMETHODIMP CValues::Clone(IEnumVARIANT **ppEnum)
{
	CComObject<CValues> * pValues;
	pValues->CreateInstance(&pValues);
	pValues->AddRef();
	pValues->m_Bstrs = this->m_Bstrs;
	*ppEnum = (IEnumVARIANT *)pValues;	
	return S_OK;
}

void CValues::Add(BSTR pVal)
{
	BSTR t = SysAllocString(pVal);
	m_Bstrs.push_back(t);
}

